package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.User;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.List_TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.LoginModel;
import cn.edu.neusoft.yuejian4.yuejian_4.model.MyModel;

/**
 * Created by Lix on 2018/10/login_bg2.
 */

public class MyBaseSetActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView back;
    private EditText nickname, realname, tel;
    private Button save;
    private SharedPreferences sp;
    private int uid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_baseset);
        initView();
        initEvent();
        sp = getSharedPreferences("userinfo", MODE_PRIVATE);
        uid = sp.getInt("uid", 4);
        MyModel myModel = new MyModel();
        myModel.userinfo(uid, userinfo);

    }

    TListener<User> userinfo = new TListener<User>() {
        @Override
        public void onResponse(User user) {
            nickname.setText(user.getNickname());
            realname.setText(user.getReal_name());
            tel.setText(Integer.toString(user.getPhone()));
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initView() {
        back = (ImageView) findViewById(R.id.base_set_back);
        nickname = (EditText) findViewById(R.id.base_ed_nickname);
        realname = (EditText) findViewById(R.id.base_ed_realname);
        tel = (EditText) findViewById(R.id.base_ed_phone);
        save = (Button) findViewById(R.id.base_set_save_btn);
    }

    private void initEvent() {
        back.setOnClickListener(this);
        save.setOnClickListener(this);
    }

    TListener<Success> updateBaseLis = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            Toast.makeText(MyBaseSetActivity.this,"修改成功", Toast.LENGTH_SHORT).show();
            finish();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_set_back:
                finish();
                break;
            case R.id.base_set_save_btn:
                String nickName = nickname.getText().toString();
                String realName = realname.getText().toString();
                int Tel = Integer.parseInt(tel.getText().toString());
                MyModel myModel = new MyModel();
                myModel.updateBase(uid, nickName, realName, Tel, updateBaseLis);
                break;
        }
    }
}
